<?php


namespace App\Tool;


class Price
{

    static function isTime($snapped, $callback = false)
    {


        $cycle = $snapped['cycle'];


        $now = time();

        switch ($cycle) {

            //一次
            case 1:


                $start_time = ($snapped['start_time']);

                if(!is_int($start_time)) $start_time=strtotime($start_time);

                $end_time = ($snapped['end_time']);

                if(!is_int($end_time)) $end_time=strtotime($end_time);

                if ($now > $start_time && $now < $end_time) {

                    if (is_callable($callback)) $callback($snapped);

                    return true;
                }

                return false;


                break;

            //每天
            case 2:


                //转换成同一天
                $nows = strtotime("2020-01-23 " . date('H:i:s', $now));

                $every_day_start_time = $snapped['every_day_start_time'];

                if(!is_int($every_day_start_time)) $every_day_start_time=strtotime($every_day_start_time);

                $every_day_end_time = $snapped['every_day_end_time'];

                if(!is_int($every_day_end_time)) $every_day_end_time=strtotime($every_day_end_time);


                if ($nows > $every_day_start_time && $nows < $every_day_end_time) {


                    if (is_callable($callback)) {

                        $callback($snapped);
                    }


                    return true;
                }



                return false;


                break;

            //每周
            case 3:


                $week_time_array = $snapped['week_time'];

                $week = (int)date("w", $now);

                if ($week == 0) $week = 7;

                if (in_array($week, $week_time_array)) {

                    if (is_callable($callback)) $callback($snapped);

                    return true;

                }

                return false;


                break;


            //每月
            case 4:

                $date_time_array = $snapped['date_time'];

                $day = date("j", $now);

                if (in_array($day, $date_time_array)) {

                    if (is_callable($callback)) $callback($snapped);

                    return true;

                }

                return false;

                break;


            default:

                return false;


        }

//        dd(11111111111111);

    }
}
